package com.haizhi.crm.sys.message.service.impl;

import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.sys.message.dao.MessageCategoryDao;
import com.haizhi.crm.sys.message.dao.MessageSettingDao;
import com.haizhi.crm.sys.message.model.MessageCategoryDo;
import com.haizhi.crm.sys.message.model.MessageSettingDo;
import com.haizhi.crm.sys.message.model.MessageSettingVo;
import com.haizhi.crm.sys.message.service.MessageSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangshan on 2018/5/16.
 */
@Service
public class MessageSettingServiceImpl implements MessageSettingService {

    private static final GLog LOG = LogFactory.getLogger(MessageSettingServiceImpl.class);

    @Autowired
    private MessageSettingDao messageSettingDao;

    @Autowired
    private MessageCategoryDao messageCategoryDao;

    @Override
    public List<MessageSettingVo> query(Long userId) {
        return query(MessageCategoryServiceImpl.ROOT_NODE_PARENT_ID, userId);
    }

    /**
     * recursive query user's message category by parentId and userId
     *
     * @param parentId
     * @param userId
     * @return
     */
    private List<MessageSettingVo> query(Long parentId, Long userId) {
        if (parentId == null) {
            parentId = MessageCategoryServiceImpl.ROOT_NODE_PARENT_ID;
        }
        List<MessageSettingVo> messageSettingVos = new ArrayList<>();
        List<MessageCategoryDo> messageCategoryDos = messageCategoryDao.findByParentId(parentId);
        for (MessageCategoryDo messageCategoryDo : messageCategoryDos) {
            Long id = messageCategoryDo.getId();
            List<MessageSettingDo> messageSettingDos = messageSettingDao.findByUserIdAndMsgCategoryId(userId, id);
            MessageSettingVo messageSettingVo = null;
            if (messageSettingDos == null || messageSettingDos.size() == 0) {
                messageSettingVo = new MessageSettingVo(id, false, messageCategoryDo.getName());
                messageSettingVo.setUserId(userId);
            } else {
                messageSettingVo = new MessageSettingVo(id, true, messageCategoryDo.getName());
                messageSettingVo.setUserId(userId);
            }
            if (!messageCategoryDo.isLeaf()) {
                //recursive query
                messageSettingVo.setMessageSettingVoList(query(id, userId));
            }
            messageSettingVo.setUserId(userId);
            messageSettingVos.add(messageSettingVo);
        }
        if (messageSettingVos == null || messageSettingVos.size() == 0) {
            return null;
        }
        return messageSettingVos;
    }

    @Override
    public void saveOrUpdate(List<MessageSettingVo> messageSettingVos) {
        Long userId = messageSettingVos.get(0).getUserId();
        if (userId == null) {
            LOG.info("userId is null" );
            return;
        }
        delete(userId);
        save(messageSettingVos);
    }

    /**
     * recursive save user's message category
     *
     * @param messageSettingVos
     */
    public void save(List<MessageSettingVo> messageSettingVos) {
        for (MessageSettingVo messageSettingVo : messageSettingVos) {
            MessageSettingDo messageSettingDo = new MessageSettingDo();
            messageSettingDo.setMsgCategoryId(messageSettingVo.getMsgCategoryId());
            messageSettingDo.setUserId(messageSettingVo.getUserId());
            if (messageSettingVo.isChecked()) {
                messageSettingDao.save(messageSettingDo);
            }
            List<MessageSettingVo> messageSettingVos1 = messageSettingVo.getMessageSettingVoList();
            if (messageSettingVos1 == null || messageSettingVos1.size() == 0) {
                ;
            } else {
                //recursive save
                save(messageSettingVos1);
            }
        }
    }

    /**
     * init user message setting when user synchronous from bank inner
     * default user own all message setting
     *
     * @param userId
     */
    @Override
    public void initUser(long userId) {
        List<MessageCategoryDo> messageCategoryDos = messageCategoryDao.findAll();
        for (MessageCategoryDo messageCategoryDo : messageCategoryDos) {
            MessageSettingDo messageSettingDo = new MessageSettingDo();
            messageSettingDo.setMsgCategoryId(messageCategoryDo.getId());
            messageSettingDo.setUserId(userId);
            messageSettingDao.save(messageSettingDo);
        }
    }


    @Override
    public void delete(long userId) {
        messageSettingDao.deleteByUserId(userId);

    }

    /**
     * judge whether the user has this message setting by userId and category name
     *
     * @param userId
     * @param categoryName
     * @return
     */
    @Override
    public boolean hasSetting(Long userId, String categoryName) {
        MessageCategoryDo messageCategoryDo = messageCategoryDao.findBySourceName(categoryName);
        List<MessageSettingDo> messageSettingDos = messageSettingDao.findByUserIdAndMsgCategoryId(userId, messageCategoryDo.getId());
        if (messageSettingDos.size() == 0) {
            return false;
        }
        return true;
    }
}
